package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.User;
import com.itheima.service.BackendMemberService;
import com.itheima.utils.DateUtils;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Lz
 * @date 2020-03-08 10:13
 * @week 星期日
 */
@RestController
@RequestMapping("/backendMember")
public class BackendMemberController {
    @Reference
    private BackendMemberService backendMemberService;

    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult page = backendMemberService.findPage(queryPageBean);
            return new Result(true, "获取会员数据成功", page);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "获取会员数据失败");
        }
    }

    @RequestMapping("/findAllHealthManager")
    public Result findAllHealthManager() {
        try {
            List<User> users = backendMemberService.findAllHealthManaget();
            return new Result(true, "查询所有健康管理师数据成功", users);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询所有健康管理师数据失败");
        }
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Map<String, Object> map) {
        try {
            Result result = backendMemberService.add(map);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增会员信息失败");
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Map data = backendMemberService.findById(id);
            return new Result(true, "查询会员信息成功", data);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询会员信息失败");
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Map<String, Object> map) {
        try {
            String s = (String) map.get("birthday");
            System.out.println(s);
            Result result = backendMemberService.edit(map);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改会员信息失败");
        }
    }

    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            backendMemberService.delete(id);
        }
        catch(RuntimeException e ){
            return new Result(false, "当前会员已生成订单，不能删除");
        }
        catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,"删除会员信息失败");
        }
        return  new Result(true,"删除会员信息成功");
    }
}
